package content;

import entity.CommodityLocal;
import entity.CommodityLocalHome;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import transfer.Commodity;

/**
 *
 * @author rebelizant, yarko, Oleh
 */
public class CommodityContent {

    public static ArrayList getMostPopularGoods(int count) {
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            Collection collection = commodityHome.findByPopularity(count);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((CommodityLocal)iterator.next()).getCommodity());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public static ArrayList getGoodsByCategory(int category, int pageCount, int count) {   
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            Collection collection = commodityHome.findByCategory(category, pageCount, count);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((CommodityLocal)iterator.next()).getCommodity());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllGoods(int pageCount, int count) {   
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            Collection collection = commodityHome.findAllGoods(pageCount, count);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((CommodityLocal)iterator.next()).getCommodity());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }    
    
    public static int getAmountGoodsOfCategory(Integer category) {   
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            return commodityHome.amountGoodsOfCategory(category);
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        return 0;
    }
    
    public static int getAmountGoods() {   
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            return commodityHome.amountGoods();
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        return 0;
    }
    
    public static Commodity getCommodity(Integer idComodity) {
        try {
            Context context = new InitialContext();
            CommodityLocalHome commodityHome = (CommodityLocalHome) context.lookup("java:comp/env/Commodity");
            CommodityLocal commodityLocal = commodityHome.findByPrimaryKey(idComodity);
            return commodityLocal.getCommodity();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
}
